//
//  UINestedScrollView1VC.swift
//  SwiftCode
//
//  Created by 于虎拴 on 2022/10/24.
//

import Foundation
import SnapKit
import MJRefresh
import RxSwift

final class UINestedScrollView1VC: UIViewController {
  private let bag = DisposeBag()
  
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    
    view.addSubview(scrollView)
    scrollView.snp.makeConstraints { make in
      make.top.leading.trailing.bottom.equalTo(view.safeAreaLayoutGuide)
    }
    
    scrollView.addSubview(headerView)
    headerView.snp.makeConstraints { make in
      make.width.equalToSuperview().inset(16)
      make.height.equalTo(200)
      make.top.equalToSuperview()
      make.leading.trailing.equalToSuperview().inset(16)
    }
    
    scrollView.addSubview(segmentedControl)
    segmentedControl.snp.makeConstraints { make in
      make.width.equalToSuperview().inset(16)
      make.height.equalTo(38)
      make.top.equalTo(headerView.snp.bottom).offset(16)
      make.leading.trailing.equalToSuperview().inset(16)
    }
    
    scrollView.addSubview(containerView)
    containerView.snp.makeConstraints { make in
      make.width.equalToSuperview().inset(16)
      make.height.equalToSuperview().offset(-70)
      make.top.equalTo(segmentedControl.snp.bottom).offset(16)
      make.leading.trailing.equalToSuperview().inset(16)
      make.bottom.equalToSuperview()
    }
    
    initData()
  }
  
  private var appear = false
  
  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    appear = true
  }
  
  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    appear = false
  }
  
  private func initData() {
    tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: { [weak self] in
      self?.delay(seconds: 2) {
        self?.tableView.mj_header?.endRefreshing()
      }
    })
    
    tableView.mj_footer = MJRefreshBackNormalFooter(refreshingBlock: { [weak self] in
      self?.delay(seconds: 2, closure: {
        self?.tableView.mj_footer?.endRefreshing {
          print("mj_footer endRefreshing")
        }
      })
    })
  }
  
  private func delay(seconds: Int, closure: @escaping ()->()) {
    let time = DispatchTime.now() + .seconds(seconds)
    DispatchQueue.main.asyncAfter(deadline: time) {
      print("🕑")
      closure()
    }
  }
  
  private lazy var scrollView: UIScrollView = {
    let scrollView = UIScrollView()
    return scrollView
  }()
  
  private lazy var contentView: UIView = {
    let view = UIView()
    return view
  }()
  
  private lazy var headerView: UIView = {
    let view = UILabel()
    view.text = "Header"
    view.textColor = .white
    view.backgroundColor = .red.withAlphaComponent(0.5)
    return view
  }()
  
  private lazy var segmentedControl: UIView = {
    let view = UIView()
    view.backgroundColor = .green.withAlphaComponent(0.5)
    return view
  }()
  
  private lazy var containerView: UIView = {
    let view = UIView()
    view.backgroundColor = .blue.withAlphaComponent(0.5)
    
    view.addSubview(tableView)
    tableView.snp.makeConstraints { make in
      make.edges.equalToSuperview()
    }
    return view
  }()
  
  private lazy var tableView: UITableView = {
    let tableView = UITableView(frame: .zero, style: .plain)
    tableView.backgroundColor = .clear
    tableView.dataSource = self
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: String(describing: UITableViewCell.self))
    return tableView
  }()
  
  let tableCount = 20
}

extension UINestedScrollView1VC: UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tableCount
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: UITableViewCell.self), for: indexPath)
    cell.textLabel?.text = "row: \(indexPath.row)"
    cell.textLabel?.textColor = .black
    cell.backgroundColor = .clear
    return cell
  }
}
